﻿using System;
using System.Collections.Generic;
using Educational.Model.Entity;
using NHibernate;
using NHibernate.Criterion;

namespace Educational.Model.EntityManager
{
    public class DepartmentManager
    {
        public static void Add(ISession session, Department an)
        {
            session.Save(an);
        }

        public static void Delete(ISession session, Department an)
        {
            session.Delete(an);
        }

        public static void Update(ISession session, Department an)
        {
            session.Update(an);
        }

        public static Department Get(ISession session, int itemId)
        {
            return session.Get<Department>(itemId);
        }

        public static Department Get1(ISession session, int schoolId, int departmentId)
        {
            ICriteria c = session.CreateCriteria(typeof(Department));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c1.Add(Expression.Eq("schoolId", schoolId));
            c.Add(Expression.Eq("departmentId", departmentId));
            return c.UniqueResult<Department>();
        }

        public static Department Get2(ISession session, int schoolId, int departmentId)
        {
            ICriteria c = session.CreateCriteria(typeof(Department));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c.SetFetchMode("positions", FetchMode.Eager);
            c1.Add(Expression.Eq("schoolId", schoolId));
            c.Add(Expression.Eq("departmentId", departmentId));
            return c.UniqueResult<Department>();
        }

        public static IList<Department> Gets1(ISession session,int schoolId)
        {
            ICriteria c = session.CreateCriteria(typeof(Department));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
                c1.Add(Expression.Eq("schoolId", schoolId));
            return c.List<Department>();
        }

        public static IList<Department> Gets2(ISession session, int schoolId,int departmentParentId)
        {
            ICriteria c = session.CreateCriteria(typeof(Department));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c1.Add(Expression.Eq("schoolId", schoolId));
            c.Add(Expression.Eq("departmentParentId", departmentParentId));
            return c.List<Department>();
        }
    }
}
